package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Score;
import com.neusoft.service.ScoreService;

import java.util.Date;
import java.text.SimpleDateFormat;

//@CrossOrigin("*") // 跨域处理
@RefreshScope
@RestController
@RequestMapping("/ScoreController")
public class ScoreController {
	
	@Autowired
	private ScoreService scoreService;
	
	// 根据用户编号获取总积分数
	@GetMapping("/getTotalScore/{userId}")
	public CommonResult<Integer> getTotalScore(@PathVariable("userId") String userId) {
		System.out.println("Using score_server_10700");
	    int totalScore = scoreService.getTotalScore(userId);
	    return new CommonResult<>(200, "success", totalScore);
	}
	
	// 根据用户编号获取积分明细
	@GetMapping("/listScoreDetail/{userId}")
	public CommonResult<List<Score>> listScoreDetail(@PathVariable("userId") String userId) {
		System.out.println("Using score_server_10700");
	    List<Score> scoreList = scoreService.listScoreDetial(userId);
	    return new CommonResult<>(200, "success", scoreList);
	}
	
	//每天24点执行一次
	@Scheduled(cron = "0 0 0 * * ?")
	public int updateAllscore() {
		// 使用SimpleDateFormat类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        String formattedDate = sdf.format(currentDate);
        System.out.println("当前系统时间（格式化）：" + formattedDate);
        scoreService.updateAtNightmare();
        return 0;
	}
}
